package com.fare.taluo.application.impl.plane.flight;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fare.taluo.application.plane.flight.FlightInfoApplication;
import com.fare.taluo.base.ddd.application.BaseApplicationImpl;
import com.fare.taluo.core.domain.config.city.PortEntity;
import com.fare.taluo.core.domain.config.line.FlightInfoEntity;
import com.fare.taluo.util.StringUtils;
import com.fare.taluo.vo.core.FlightInfoVO;

@Transactional
@Scope("singleton")
@Service("flightInfoApplication")
public class FlightInfoApplicationImpl extends BaseApplicationImpl implements FlightInfoApplication {


	@Override
	public List<FlightInfoVO> findByLine(String fromCityCode, String toCityCode) {
		List<FlightInfoEntity> list = super.queryChannel().queryResult("from FlightInfoEntity e where e.fromCity=? and e.toCity=?",
				new Object[]{fromCityCode,toCityCode});
		return this.entityToVo(list);
	}

	@Override
	public List<FlightInfoVO> findByFlightNo(String flightNo) {
		List<FlightInfoVO> lists=new ArrayList<FlightInfoVO> ();
		FlightInfoEntity flightInfoEntity=new FlightInfoEntity();
		List<FlightInfoEntity> voList=flightInfoEntity.findByFlightNo(flightNo);
		if(voList!=null){
			if(voList.size()>0){
				for (FlightInfoEntity flightInfoEntity2 : voList) {
					FlightInfoVO vo=new FlightInfoVO();
					vo.setFlightNo(flightInfoEntity2.getFlightNo());
					vo.setCarrierKey(flightInfoEntity2.getCompanyCode());
					vo.setFromCity(flightInfoEntity2.getFromCity());
					vo.setFromCityName(flightInfoEntity2.getFromCityName());
					vo.setToCity(flightInfoEntity2.getToCity());
					vo.setToCityName(flightInfoEntity2.getToCityName());
					vo.setCompanyCode(flightInfoEntity2.getCompanyCode());
					vo.setCarrierName(flightInfoEntity2.getCompanyName());
					vo.setCompanyName(flightInfoEntity2.getCompanyName());
					vo.setAreaCode(flightInfoEntity2.getAreaCode());
					lists.add(vo);
				}
			}
		}
		
		return lists;
	}

	@Override
	public List<FlightInfoVO> findByLine(String fromCityCode,
			String toCityCode, String air_key) {
		FlightInfoEntity flightInfoEntity=new FlightInfoEntity();
		List<FlightInfoEntity> voList=flightInfoEntity.findByLine(fromCityCode,toCityCode,air_key);
		return this.entityToVo(voList);
	}
	
	@Override
	public List<FlightInfoVO> findByLine(List<PortEntity> formCitys,
			List<PortEntity> toCitys, String carrier_key) {
		FlightInfoEntity flightInfoEntity=new FlightInfoEntity();
		List<FlightInfoEntity> voList=flightInfoEntity.findByLine(formCitys,toCitys,carrier_key);
		return this.entityToVo(voList);
	}
	
	public List<FlightInfoVO> findLineByCodes(String fromCityCode, String toCityCode, String carrier_key) {
		FlightInfoEntity flightInfoEntity=new FlightInfoEntity();
		List<FlightInfoEntity> voList=flightInfoEntity.findLineByCodes(fromCityCode, toCityCode,carrier_key);
		return this.entityToVo(voList);
	}
	
	@Override
	public List<FlightInfoVO> findByLineAndDate(String fromCityCode,
			String toCityCode, String date) {
		FlightInfoEntity flightInfoEntity=new FlightInfoEntity();
		List<FlightInfoEntity> voList=flightInfoEntity.findByLineAndDate(fromCityCode,toCityCode,date);
		return this.entityToVo(voList);
	}
	
	public List<FlightInfoVO> findByLineCodeNotIn (String fromCityCode,String toCityCode,String companyCodeNotIn){
		FlightInfoEntity flightInfoEntity=new FlightInfoEntity();
		List<FlightInfoEntity> voList=flightInfoEntity.findByLineCodeNotIn(fromCityCode,toCityCode,companyCodeNotIn);
		return this.entityToVo(voList);
	}
	private List<FlightInfoVO> entityToVo(List<FlightInfoEntity> voList){
		List<FlightInfoVO> lists=new ArrayList<FlightInfoVO>();
		if(voList!=null){
			if(voList.size()>0){
				int i = 0;
				for (FlightInfoEntity flightInfoEntity2 : voList) {
					i++;
					FlightInfoVO vo=new FlightInfoVO();
					vo.setId(i+"");
					vo.setFlightNo(flightInfoEntity2.getFlightNo());
					//vo.setFromCity(flightInfoEntity2.getFromCity());
					if(flightInfoEntity2.getFromCityName().contains("PEK")){
						vo.setFromCityName(flightInfoEntity2.getFromCityName());
					}else{
						vo.setFromCityName(flightInfoEntity2.getFromCityName()+"("+flightInfoEntity2.getFromCity()+")");
					}
					
					//vo.setToCity(flightInfoEntity2.getToCity());
					if(flightInfoEntity2.getToCityName().contains("PEK")){
						vo.setToCityName(flightInfoEntity2.getToCityName());
					}else{
						vo.setToCityName(flightInfoEntity2.getToCityName()+"("+flightInfoEntity2.getToCity()+")");
					}
					
					vo.setCompanyCode(flightInfoEntity2.getCompanyCode());
					vo.setCompanyName(flightInfoEntity2.getCompanyName());
					vo.setCarrierKey(flightInfoEntity2.getCompanyCode());
					vo.setCarrierName(flightInfoEntity2.getCompanyName());
					vo.setAreaCode(flightInfoEntity2.getAreaCode());
					vo.setAreaName(flightInfoEntity2.getAreaName());
					vo.setFlightStartTime(flightInfoEntity2.getFlightStartTime());
					vo.setFlightEndTime(flightInfoEntity2.getFlightEndTime());
					vo.setFrequence(flightInfoEntity2.getFrequence());
					vo.setEntryTime(flightInfoEntity2.getEntryTime());
					vo.setFirstShareFlight(flightInfoEntity2.getFirstShareFlight());
					vo.setTransgerFlightNo(flightInfoEntity2.getTransgerFlightNo());
					if(null!=flightInfoEntity2.getTransferCityName()&&flightInfoEntity2.getTransferCityName().contains("PEK")){
						vo.setTransferCityName(flightInfoEntity2.getTransferCityName());
					}else if(null!=flightInfoEntity2.getTransferCityName()){
						vo.setTransferCityName(flightInfoEntity2.getTransferCityName()+"("+flightInfoEntity2.getTransferCity()+")");
					}
					lists.add(vo);
				}
			}
		}
		
		return lists;
	}

	@Override
	public void save(FlightInfoVO vo) {
		FlightInfoEntity flight = new FlightInfoEntity();
		flight.setFlightNo(vo.getFlightNo());
		flight.setFrequence(vo.getFrequence());
		flight.setFromCityName(vo.getFromCityName());
		flight.setFromCity(vo.getFromCity());
		flight.setToCityName(vo.getToCityName());
		flight.setToCity(vo.getToCity());
		flight.setFlightStartTime(vo.getFlightStartTime());
		flight.setFlightEndTime(vo.getFlightEndTime());
		flight.setAreaCode(vo.getAreaCode());
		flight.setAreaName(vo.getAreaName());
		flight.setCompanyCode(vo.getCompanyCode());
		flight.setCompanyName(vo.getCompanyName());
		flight.setEntryTime(vo.getEntryTime());
		flight.setEffectStartTime(vo.getEffectStartTime());
		flight.setEffectEndTime(vo.getEffectEndTime());
		flight.setFirstClass(vo.getFirstClass());
		flight.setSecondClass(vo.getSecondClass());
		flight.setFirstFlightType(vo.getFirstFlightType());
		flight.setSecondFlightType(vo.getSecondFlightType());
		flight.setFirstShareFlight(vo.getFirstShareFlight());
		flight.setSecondShareFlight(vo.getSecondShareFlight());
		flight.setTransferCity(vo.getTransferCity());
		flight.setTransferCityName(vo.getTransferCityName());
		flight.setTransferStartTime(vo.getTransferStartTime());
		flight.setTransferEndTime(vo.getTransferEndTime());
		flight.setTransgerFlightNo(vo.getTransgerFlightNo());
		flight.setStopCity(vo.getStopCity());
		flight.setFlightDate(vo.getFlightDate());
		try {
			flight.save(flight);
		} catch (Exception e) {
			e.printStackTrace();
		}
	    
	}

	@Override
	public List<FlightInfoVO> findByConditions(String companyCode,
			String fromCityCode, String toCityCode,String flightNo, Date entryTime) {
		FlightInfoEntity flightInfoEntity=new FlightInfoEntity();
		List<FlightInfoEntity> voList=flightInfoEntity.findByConditions(companyCode, fromCityCode, toCityCode,flightNo, entryTime);
		return this.entityToVo(voList);
	}

	//该方法有两个地方调用，同时某些地方有重复使用该方法代码，如：CommonAction.outCityFlightNo()；统一修改和调用，bug：如查北京的时候 ：北京应该有两个PortEntity ，修改后可以同时查询两个机场的航班 update by lyq
	@Override
	public List<FlightInfoVO> findByConditions(String companyCarrierKey,
			String fromCityName, String toCityName) {
		String formCityCode=null,toCityCode=null;
		String fromCityNameSub = fromCityName.split("\\(")[0];
		String toCityNameSub = toCityName.split("\\(")[0];
		formCityCode=fromCityName.substring(fromCityName.indexOf("(")+1, fromCityName.lastIndexOf(")"));
		toCityCode=toCityName.substring(toCityName.indexOf("(")+1, toCityName.lastIndexOf(")"));
		PortEntity pe=new PortEntity();
		if(formCityCode!=null && !"".equals(formCityCode) && toCityCode !=null && !"".equals(toCityCode)){
			return generateOnly(this.findLineByCodes(formCityCode, toCityCode, companyCarrierKey));
		}
		
		 List<PortEntity> formCitys = StringUtils.isNotEmpty(fromCityNameSub) ? pe
					.findPortEntityByCityName(fromCityNameSub) : null;
			List<PortEntity> toCitys = StringUtils.isNotEmpty(toCityNameSub) ? pe
					.findPortEntityByCityName(toCityNameSub) : null;
		return  generateOnly(this.findByLine(formCitys, toCitys, companyCarrierKey));
//					PortEntity pe=new PortEntity();
//		PortEntity from=pe.findPortEntityByCityName(fromCityName);
//		PortEntity to=pe.findPortEntityByCityName(toCityName);
//		FlightInfoEntity flightInfoEntity=new FlightInfoEntity();
//		return this.entityToVo(flightInfoEntity.findByLine(from.getPortCode(), to.getPortCode(), companyCarrierKey));
	}
	
	/**
	 * 
	 * 该方法在PriceUtil.java存在 ，为什么该类放在web工程下？而且在 CommonAction 有copy该方法代码（已删） update by lyq 
	 * 排重，航班号会重复出现
	 */
	private List<FlightInfoVO> generateOnly(List<FlightInfoVO> vos) {
		List<FlightInfoVO> finalResult = new ArrayList<FlightInfoVO>();
		Set<String> set = new HashSet<String>();
		for (FlightInfoVO vo : vos) {
			if (set.add(vo.getFlightNo()) == true) {
				finalResult.add(vo);
			}
		}
		return finalResult;
	}


}
